package com.qj.yantaishan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.domain.Store;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 店铺Mapper接口
 *
 * @author qihe
 * @date 2022-07-22
 */
public interface StoreMapper extends BaseMapper<Store> {
    /**
     * 查询店铺
     *
     * @param storeId 店铺主键
     * @return 店铺
     */
    public Store selectStoreByStoreId(Integer storeId);

    /**
     * 查询店铺列表
     *
     * @param store 店铺
     * @return 店铺集合
     */
    public List<Store> selectStoreList(Store store);

    /**
     * 新增店铺
     *
     * @param store 店铺
     * @return 结果
     */
    public int insertStore(Store store);

    /**
     * 修改店铺
     *
     * @param store 店铺
     * @return 结果
     */
    public int updateStore(Store store);


    /**
     * @return
     */
    @Select("select name,logo from shanglala_store")
    Map<String, String> getStoreMap();

    @Select("select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}")
    int selectYesterdayRegMember(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select count(*) from shanglala_order where add_time  >#{startTime} and add_time <#{endTime}")
    int selectYesterdayOrderMember(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select(" select count(*) from shanglala_order where status in (2,6)")
    int selectWaitOrderNumber();

    @Select("select count(*) from shanglala_product where store_id = 1 and status =1 and soldout=0 and  quantity >0")
    int selectProductNumber();

    @Select("select count(*) from shanglala_store_analytics  where store_id =1 and visited_time  >#{startTime} and visited_time <#{endTime}")
    int selectNowDayFlowNumber(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select count(*) from shanglala_user where reg_time >#{startTime} and reg_time <#{endTime}")
    int selectNowDayNewMemberNumber(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select ifnull(sum((srp.pro_price*srp.pro_num)*1),0)  from shanglala_return  sr inner join  shanglala_return_product  srp on sr.id = srp.return_id  where dateline>#{startTime} and dateline<#{endTime} and  sr.status in(3,5)")
    int selectNowDayReturnOrderNumber(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select(" select ifnull(sum(pay_money),0) from shanglala_order  where paid_time>#{startTime} and paid_time<#{endTime}")
    BigDecimal selectNowDayPayMoney(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

    @Select("select distinct sp.name ,sum(sop.pro_num) sale from shanglala_order_product sop  inner join shanglala_product sp  on sop.product_id= sp.product_id where order_id in (select order_id from shanglala_order where add_time >#{startTime} and add_time<#{endTime} )  GROUP BY sop.order_id  order by  sale desc limit 1,10")
    List<Map<String, Object>> selectYesterdaySaleProduct(@Param("startTime") Long startTime, @Param("endTime") Long endTime);
    @Select("select distinct sp.name ,sum(sop.pro_num) sale from shanglala_order_product sop  inner join shanglala_product sp  on sop.product_id= sp.product_id where order_id in (select order_id from shanglala_order where add_time >#{startTime} and add_time<#{endTime} )  GROUP BY sop.order_id   order by  sale desc limit 1,10")

    List<Map<String, Object>> selectWeekSaleProduct(@Param("startTime") Long startTime, @Param("endTime") Long endTime);
    @Select("select distinct sp.name ,sum(sop.pro_num) sale from shanglala_order_product sop  inner join shanglala_product sp  on sop.product_id= sp.product_id where order_id in (select order_id from shanglala_order where add_time >#{startTime} and add_time<#{endTime} )  GROUP BY sop.order_id   order by  sale desc limit 1,10")
    List<Map<String, Object>> selectMountSaleProduct(@Param("startTime") Long startTime, @Param("endTime") Long endTime);
@Select("\n" +
        "select ((select count(distinct uid) from shanglala_order where add_time <#{endTime} and add_time > #{startTime}   )" +
        "/count(*))*100 from shanglala_user where agent_id =8581960447  and reg_time< #{endTime}")
    double getPayMount(@Param("startTime") Long startTime, @Param("endTime") Long endTime);

}
